package com.spi.controller;

import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.common.PDMetadata;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

@RequestMapping("/pdf")
@RestController
public class PdfController {

    @RequestMapping("/test01")
    public void test01(){
        // 从配置文件中读取文件路径
        //String inputPath = getFilePathFromConfig("inputPath");
        //String outputPath = getFilePathFromConfig("outputPath");
        String inputPath = "D:\\555\\pdftest\\文档1.pdf";
        String outputPath = "D:\\555\\pdftest\\1.pdf";
        try (PDDocument document = PDDocument.load(new File(inputPath))) {
            // 压缩图像
            compressImages(document,0.4f);

            // 删除不必要的元数据
            removeMetadata(document);

            // 保存压缩后的PDF文档
            document.save(outputPath);
        } catch (IOException e) {
            // 记录日志或提供用户友好的错误信息
            System.err.println("处理PDF文档时发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
    private static void removeMetadata(PDDocument document) {
        PDMetadata metadata = document.getDocumentCatalog().getMetadata();
        if (metadata != null) {
            document.getDocumentCatalog().setMetadata(null);
        }
    }
    private static void compressImages(PDDocument document,float quality) throws IOException {
        PDPageTree pages = document.getDocumentCatalog().getPages();
        for (PDPage page : pages) {
            for (COSName name : page.getResources().getXObjectNames()) {
                if (name != null) {
                    Object xObject = page.getResources().getXObject(name);
                    if (xObject instanceof PDImageXObject) {
                        PDImageXObject image = (PDImageXObject) xObject;
                        BufferedImage bufferedImage = image.getImage();
                        File tempFile = File.createTempFile("temp", ".jpg");
                        ImageIO.write(bufferedImage, "JPEG", tempFile);
                        // 读取压缩后的图像并创建 PDImageXObject
                        try (FileInputStream fis = new FileInputStream(tempFile)) {
                            PDImageXObject compressedImage = JPEGFactory.createFromImage(document, ImageIO.read(fis),quality);
                            page.getResources().put(name, compressedImage);
                        }
                        tempFile.delete();
                    }
                }
            }
        }
    }
}
